package com.thws.boyaddz;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class MenuView extends View implements OnTouchListener {

	Bitmap bgBitmap;
	Context context;
	private int x = 270; //卡片左侧x坐标
	private int y = 50;//卡片顶部y坐标
	private Bitmap[] menuItemBitmaps;
	public MenuView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	private void init() {
		menuItemBitmaps = new Bitmap[5]; //一张背景图，5张选项卡
		bgBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_bg);
		menuItemBitmaps[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu1); //开始游戏
		menuItemBitmaps[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu2);//游戏帮助
		menuItemBitmaps[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu3);//游戏关于
		menuItemBitmaps[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu4);//声音设置
		menuItemBitmaps[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu5);//退出游戏

		this.setOnTouchListener(this); //点击事件
	}


	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("xx","执行");

		Rect src = new Rect(); //图片裁剪后，保留部分的矩形
		Rect des = new Rect(); //图片要显示的位置矩形
		src.set(0, 0, bgBitmap.getWidth(), bgBitmap.getHeight()); //设置坐标
		// System.out.println("menu:" + background.getWidth() + "X" +
		// background.getHeight());
		des.set(0, 0, MainActivity.SCREEN_WIDTH, MainActivity.SCREEN_HEIGHT);
		Paint paint = new Paint();
		canvas.drawBitmap(bgBitmap, src, des, paint); //绘制背景图片，铺满全屏
		for (int i = 0; i < menuItemBitmaps.length; i++) { //绘制5个按钮
			canvas.drawBitmap(
					menuItemBitmaps[i],
					(int) (x * MainActivity.SCALE_HORIAONTAL),
					(int) ((y + i * 43) * MainActivity.SCALE_VERTICAL),
					paint
			);
		}

	}



	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int ex = (int) event.getX();
		int ey = (int) event.getY();
		System.out.println(event.getX() + "," + event.getY());
		int selectIndex = -1;
		for (int i = 0; i < menuItemBitmaps.length; i++) {
			System.out.println(x + "  " + (y + i * 43));
			boolean inRect = CardsManager.inRect(
					ex, //横坐标
					ey, //纵坐标
					(int) (x * MainActivity.SCALE_HORIAONTAL), //矩形左侧坐标, 270 * 4.19= 1131.3
					(int) ((y + i * 43) * MainActivity.SCALE_VERTICAL), //矩形上边纵坐标，（150+0*43）* 3.38=507
					(int) (125 * MainActivity.SCALE_HORIAONTAL), //矩形宽度
					(int) (33 * MainActivity.SCALE_VERTICAL) //矩形高度
			);
			if (inRect) {
				selectIndex = i;
				break;
			}
		}
		System.out.println(selectIndex);
		switch (selectIndex) {
			case 0 : //开始游戏
				MainActivity.handler.sendEmptyMessage(MainActivity.GAME);
				break;
			case 1 : //游戏帮助
				break;
			case 2 : //游戏关于
				break;
			case 3 : //声音设置
				break;
			case 4 : //退出游戏
				MainActivity.handler.sendEmptyMessage(MainActivity.EXIT);
				break;
		}
		return super.onTouchEvent(event);
	}
}
